package com.bonc.rms.service.impl.tms;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bonc.odk.logmanager.LogManager;
import com.bonc.rms.dao.tms.IOrganizationDao;
import com.bonc.rms.service.tms.IOrganizationService;

@Service
public class OrganizationServiceImpl implements IOrganizationService{

	@Autowired
	IOrganizationDao organizationDao;

	@Override
	public List<Map<String, Object>> getOrganizationById(Integer depId, String date) {
		LogManager.Method("In  Service : OrganizationServiceImpl -> getOrganizationById");
		Date sqlDate = null;
		if(date == null) {
			sqlDate = new Date(new java.util.Date().getTime());
		} else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				sqlDate = new Date(sdf.parse(date).getTime());
			} catch (ParseException e) {
				LogManager.Exception("error when parse string to date", e);
			}
		}
		LogManager.Method("Out Service : OrganizationServiceImpl -> getOrganizationById");
		return organizationDao.getOrganizationById(depId, sqlDate);
	}

	@Override
	public List<Map<String, Object>> getOrganizationByParentId(Integer depId, String date) {
		LogManager.Method("In  Service : OrganizationServiceImpl -> getOrganizationByParentId");
		Date sqlDate = null;
		if(date == null) {
			sqlDate = new Date(new java.util.Date().getTime());
		} else {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				sqlDate = new Date(sdf.parse(date).getTime());
			} catch (ParseException e) {
				LogManager.Exception("error when parse string to date", e);
			}
		}
		LogManager.Method("Out Service : OrganizationServiceImpl -> getOrganizationByParentId");
		return organizationDao.getOrganizationByParentId(depId, sqlDate);
	}

	@Override
	public List<String> getHistoryDate() {
		return organizationDao.getHistoryDate();
	}

	@Override
	public boolean isLeaf(Integer depId) {
		return organizationDao.isLeaf(depId);
	}

}
